package com.logansoft.zhxypkoa.components;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.event.AbstractRepositoryEventListener;
import org.springframework.stereotype.Component;

import com.logansoft.zhxypkoa.domain.Base;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class DomainListener extends AbstractRepositoryEventListener<Base> {

	@Autowired
	private HttpSession httpSession;

	@Override
	protected void onBeforeCreate(Base entity) {
		// TODO Auto-generated method stub
		super.onBeforeCreate(entity);
		String userId = httpSession.getAttribute("userId").toString();
		entity.setCreatedAt(new Date());
		entity.setCreatedBy(userId);
	}

	@Override
	protected void onAfterCreate(Base entity) {
		// TODO Auto-generated method stub
		super.onAfterCreate(entity);
		log.debug("onAfterCreate: " + entity.getClass() + ", " + entity.getId());
	}

	@Override
	protected void onBeforeSave(Base entity) {
		// TODO Auto-generated method stub
		super.onBeforeSave(entity);
		log.debug("onBeforeSave: " + entity.getClass() + ", " + entity.getId());
		String userId = httpSession.getAttribute("userId").toString();
		entity.setUpdatedAt(new Date());
		entity.setUpdatedBy(userId);
	}

	@Override
	protected void onAfterSave(Base entity) {
		// TODO Auto-generated method stub
		super.onAfterSave(entity);
		log.debug("onAfterSave: " + entity.getClass() + ", " + entity.getId());
	}

	@Override
	protected void onBeforeLinkSave(Base parent, Object linked) {
		// TODO Auto-generated method stub
		super.onBeforeLinkSave(parent, linked);
		log.debug("onBeforeLinkSave: " + parent.getClass() + ", " + parent.getId() + ", " + linked);
	}

	@Override
	protected void onAfterLinkSave(Base parent, Object linked) {
		// TODO Auto-generated method stub
		super.onAfterLinkSave(parent, linked);
		log.debug("onAfterLinkSave: " + parent.getClass() + ", " + parent.getId() + ", " + linked);
	}

	@Override
	protected void onBeforeLinkDelete(Base parent, Object linked) {
		// TODO Auto-generated method stub
		super.onBeforeLinkDelete(parent, linked);
		log.debug("onBeforeLinkDelete: " + parent.getClass() + ", " + parent.getId() + ", " + linked);
	}

	@Override
	protected void onAfterLinkDelete(Base parent, Object linked) {
		// TODO Auto-generated method stub
		super.onAfterLinkDelete(parent, linked);
		log.debug("onAfterLinkDelete: " + parent.getClass() + ", " + parent.getId() + ", " + linked);
	}

	@Override
	protected void onBeforeDelete(Base entity) {
		// TODO Auto-generated method stub
		super.onBeforeDelete(entity);
		log.debug("onBeforeDelete: " + entity.getClass() + ", " + entity.getId());
	}

	@Override
	protected void onAfterDelete(Base entity) {
		// TODO Auto-generated method stub
		super.onAfterDelete(entity);
		log.debug("onAfterDelete: " + entity.getClass() + ", " + entity.getId());
	}

}
